﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MotorcycleStore.Repository;
using System.Drawing;
using MotorcycleStore.DomainModel;
using System.IO;
using System.Drawing.Imaging;

namespace MotorcycleStore.MVC.Areas.Administration.Controllers
{
	public class PhotoController : Controller
	{
		IPhotoRepository repository;

		public PhotoController(IPhotoRepository repository)
		{
			this.repository = repository;
		}

		public ActionResult Index(int? id)
		{
			if (!id.HasValue)
				return RedirectToAction("Upload");

			var photo = repository.Photos.SingleOrDefault(p => p.Id == id);
			if (photo == null)
				return Redirect("~/img/no-photo.jpg");

			return File(photo.PhotoData, photo.ContentType);
		}

		public ViewResult Edit(int id)
		{
			var photo = repository.Photos.SingleOrDefault(p => p.Id == id);
			return View(photo);
		}

		[HttpPost]
		public ActionResult Edit(int id, FormCollection form)
		{
			var photo = repository.Photos.SingleOrDefault(p => p.Id == id);

			// TODO: UpdateModel()

			return View(photo);
		}
	}
}
